<?php  if ( ! defined('__CORE_PATH__')) exit('No direct script access allowed');
/* SVN FILE: $Id: app_controller.php 25 2008-08-29 21:58:28Z namaless $ */
/**
 * Benchmark Class
 *
 * Permette il calcolo delle prestazioni dell'applicazione.
 *
 * @author			Pereira Pulido Nuno Ricardo | Namaless | namaless@gmail.com
 * @copyright		Copyright 1981-2008, Namaless.
 * @link			http://www.namaless.com Namaless Blog
 * @version			1.0
 * @version			$Revision: 25 $
 * @modifiedby		$LastChangedBy: namaless $
 * @lastmodified	$Date: 2008-08-29 23:58:28 +0200 (ven, 29 ago 2008) $
 * @license			http://www.opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * Classe Principale.
 */
class Benchmark
{
	var $marker = array();

	/**
	 * Singleton Pattern.
	 *
	 * @var object
	 **/
	private static $instance = NULL;

	// --------------------------------------------------------------------

	/**
	 * Implemento il Singleton.
	 *
	 * @return	object
	 */
	public static function get_instance()
	{
		if ( self::$instance == NULL )
		{   
			$c = __CLASS__;
		    self::$instance =& new $c;
		}
		
		return self::$instance;
	}

	// --------------------------------------------------------------------

	private function __construct(){}
	
	// --------------------------------------------------------------------

	private function __clone(){}

	// --------------------------------------------------------------------

	public function __destruct(){}
	
	/**
	 * Implemento il Factory.
	 */
	public static function factory(){}

	// --------------------------------------------------------------------

	/**
	 * Set a benchmark marker
	 *
	 * Multiple calls to this function can be made so that several
	 * execution points can be timed
	 *
	 * @param	string	$name	name of the marker
	 * @return	void
	 */
	public static function mark($name)
	{
		self::get_instance()->marker[$name] = microtime();
	}
  	
	// --------------------------------------------------------------------

	/**
	 * Calculates the time difference between two marked points.
	 *
	 * If the first parameter is empty this function instead returns the
	 * {elapsed_time} pseudo-variable. This permits the full system
	 * execution time to be shown in a template. The output class will
	 * swap the real value for this variable.
	 *
	 * @param	string	a particular marked point
	 * @param	string	a particular marked point
	 * @param	integer	the number of decimal places
	 * @return	mixed
	 */
	public static function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
	{
		// Creo l'oggetto.
		$benchmark = self::get_instance();

		if ( $point1 == '' )
		{
			return '{elapsed_time}';
		}

		if ( ! isset($benchmark->marker[$point1]) )
		{
			return '';
		}

		if ( ! isset($benchmark->marker[$point2]) )
		{
			$benchmark->marker[$point2] = microtime();
		}

		list($sm, $ss) = explode(' ', $benchmark->marker[$point1]);
		list($em, $es) = explode(' ', $benchmark->marker[$point2]);

		return number_format(($em + $es) - ($sm + $ss), $decimals);
	}

	// --------------------------------------------------------------------

	/**
	 * Memory Usage
	 *
	 * This function returns the {memory_usage} pseudo-variable.
	 * This permits it to be put it anywhere in a template
	 * without the memory being calculated until the end.
	 * The output class will swap the real value for this variable.
	 *
	 * @return	string
	 */
	public static function memory_usage()
	{
		return '{memory_usage}';
	}

}
